<?php

namespace app\common\model\Order;

use think\Model;
use think\facade\Config;
use think\model\concern\SoftDelete;
/**
 * 订单商品模型
 */
class Product extends Model
{
    //继承软删除
    use SoftDelete;
    
    //模型对应的是哪张表
    protected $table = "pre_order_product";

    //开启自动写入
    protected $autoWriteTimestamp = true;

    //设置字段的名字
    protected $createTime = false; //插入的时候设置的字段名

    //禁止 写入的时间字段
    protected $updateTime = "updatetime";

    // 软删除的字段
    protected $deleteTime = 'deletetime';

    //如果想要在查询的时候显示出，获取器自定义的字段
    protected $append = [
        'proimg_cdn', //多张商品评价图
    ];

    // 转换商品评价图片
    public function getProimgCdnAttr($value, $data)
    {
        //获取的是 config.php 当中 cdn选项
        $cdn = Config::get('app.cdn');

        // $thumbs = empty($data['thumbs']) ? [] : explode(',', $data['thumbs']);
        $thumbs = empty($data['proimg']) ? [] : trim($data['proimg']);

        if(empty($thumbs))
        {
            return [];
        }

        $thumbs = str_replace("uploads/", $cdn."/uploads/", $thumbs);

        //在转换为数组
        $list = explode(',', $thumbs);

        return $list;
    }


    //关联查询
    public function info()
    {
        return $this->belongsTo(\app\common\model\Product\Product::class, 'proid', 'id');
    }

    // 订单
    public function order()
    {
        return $this->belongsTo(\app\common\model\Order\Order::class, 'orderid', 'id');
    }

    // 用户
    public function user()
    {
        //belongsTo 关联查询
        // belongsTo('关联模型名','外键名','关联表主键名');
        return $this->belongsTo('app\common\model\User\User', 'userid', 'id');
    }
}
